/**
 *  Copyright (C) 2008-2013  Telosys project org. ( http://www.telosys.org/ )
 *
 *  Licensed under the GNU LESSER GENERAL PUBLIC LICENSE, Version 3.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *          http://www.gnu.org/licenses/lgpl.html
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package org.telosys.common.dataset;

import java.io.BufferedReader;
import java.io.InputStream;
import java.util.Iterator;
import java.util.List;

import org.telosys.common.TelosysRuntimeException;
import org.telosys.util.file.GenericFileParser;

/**
 * DataSet loader for file data <br>
 * A file parser is used to load the file content, the the file content is loaded in the DataSet
 * 
 * @author Laurent GUERIN 
 *  
 */
public class DataSetFileLoader
{
	
	private GenericFileParser _fileParser = null ;

	//-----------------------------------------------------------------------------
    //--- CONSTRUCTORS
    //-----------------------------------------------------------------------------

    public DataSetFileLoader(GenericFileParser fileParser) //throws TelosysException
    {
        super();
        if ( fileParser == null ) throw new TelosysRuntimeException("File parser is null");
        _fileParser = fileParser ;
    }

//    public FileDataSet(GenericFileParser fileParser, String fileName) throws TelosysException
//    {
//        super();
//        if ( fileParser == null ) throw new TelosysException("File parser is null");
//        if ( fileName   == null ) throw new TelosysException("File name is null");
//        _fileParser = fileParser ;
//        _fileName   = fileName ;
//    }
//    
//    public FileDataSet(GenericFileParser fileParser, InputStream inputStream) throws TelosysException
//    {
//        super();
//        if ( fileParser  == null ) throw new TelosysException("File parser is null");
//        if ( inputStream == null ) throw new TelosysException("InputStream is null");
//        _fileParser  = fileParser ;
//        _inputStream = inputStream ;
//    }
//
//    public FileDataSet(GenericFileParser fileParser, BufferedReader bufferedReader) throws TelosysException
//    {
//        super();
//        if ( fileParser     == null ) throw new TelosysException("File parser is null");
//        if ( bufferedReader == null ) throw new TelosysException("BufferedReader is null");
//        _fileParser     = fileParser ;
//        _bufferedReader = bufferedReader ;
//    }


//    //-----------------------------------------------------------------------------
//    /* (non-Javadoc)
//     * @see org.objectweb.telosys.common.data.DataSet#load()
//     */
//    public int load() throws TelosysException
//    {
//    	if ( _fileName != null ) {
//        	return load( _fileParser, _fileName) ;
//    	}
//    	else if ( _inputStream != null ) {
//        	return load( _fileParser, _inputStream) ;
//    	}
//    	else if ( _bufferedReader != null ) {
//        	return load( _fileParser, _bufferedReader) ;
//    	}
//    	else {
//    		throw new TelosysException("No input defined, cannot load without input parameter");
//    	}
//    }

//    //-----------------------------------------------------------------------------
//    /* (non-Javadoc)
//     * @see org.objectweb.telosys.common.data.DataSet#load(java.lang.Object)
//     */
//    public int load(Object obj) throws TelosysException
//    {
//        if ( obj == null )
//        {
//            throw new TelosysException("load(object) : parameter is null");
//        }
//        
//        if ( obj instanceof String )
//        {
//        	return load( _fileParser, (String)obj ) ;
//        }
//        else  if ( obj instanceof InputStream )
//        {
//        	return load( _fileParser, (InputStream)obj ) ;
//        }
//        else  if ( obj instanceof BufferedReader )
//        {
//        	return load( _fileParser, (BufferedReader)obj ) ;
//        }
//        else
//        {
//            throw new TelosysException("load(obj) : unusable parameter, class = " + obj.getClass().getName() );
//        }
//    }

    //-----------------------------------------------------------------------------
    //-----------------------------------------------------------------------------
    public DataSet load(String fileName) 
    {
    	DataSet dataset = new DataSet();
    	load(dataset, fileName);
    	return dataset ;
    }
    //-----------------------------------------------------------------------------
    public int load(DataSet dataset, String fileName) 
    {
//        //--- Initialize the DataSet ( prepare to load )
//        dataset.initBeforeLoad();
        //--- Load ...
        try
        {
            //--- Parse with file name
        	_fileParser.parse(fileName);            
        } catch (Throwable e)
        {
        	throw new TelosysRuntimeException("Cannot parse file", e);
        }
        return storeDataRows( dataset, _fileParser.getResult() );
    }
    //-----------------------------------------------------------------------------
    //-----------------------------------------------------------------------------
    public DataSet load(InputStream inputStream) //throws TelosysException
    {
    	DataSet dataset = new DataSet();
    	load(dataset, inputStream);
    	return dataset ;
    }
    //-----------------------------------------------------------------------------
    public int load(DataSet dataset, InputStream inputStream) //throws TelosysException
    {
//        //--- Initialize the DataSet ( prepare to load )
//        dataset.initBeforeLoad();
        //--- Load ...
        try
        {
            //--- Parse with file name
        	_fileParser.parse(inputStream);            
        } catch (Throwable e)
        {
        	throw new TelosysRuntimeException("Cannot parse file", e);
        }
        return storeDataRows( dataset, _fileParser.getResult() );
    }
    //-----------------------------------------------------------------------------
    //-----------------------------------------------------------------------------
    public DataSet load(BufferedReader bufferedReader) 
    {
    	DataSet dataset = new DataSet();
    	load(dataset, bufferedReader);
    	return dataset ;    	
    }
    //-----------------------------------------------------------------------------
    public int load(DataSet dataset, BufferedReader bufferedReader) 
    {
//        //--- Initialize the DataSet ( prepare to load )
//        dataset.initBeforeLoad();
        //--- Load ...
        try
        {
            //--- Parse with file name
        	_fileParser.parse(bufferedReader);            
        } catch (Throwable e)
        {
        	throw new TelosysRuntimeException("Cannot parse file", e);
        }
        return storeDataRows( dataset, _fileParser.getResult() );
    }
    
    //-----------------------------------------------------------------------------
    //-----------------------------------------------------------------------------
    private int storeDataRows(DataSet dataset, List<Object> rows) //throws TelosysException
    {
    	Object o ;
    	int n = 0 ;

    	//for ( Object o : rows ) {
    	Iterator<Object> iter = rows.iterator();
    	while ( iter.hasNext() ) {
    		o = iter.next();
        	if ( o instanceof DataRow ) 
        	{
        		dataset.addRow((DataRow) o);
                n++;
        	}
        	else if ( o instanceof Object[] )
        	{
                Object[] values = (Object[]) o ;
                DataRow dr = new DataRow( values.length, values);
                dataset.addRow(dr);  
                n++;
        	}
        	else 
        	{
                throw new TelosysRuntimeException("unusable row, class = " + o.getClass().getName() );        		
        	}
        }
        return n ;
    }
    //-----------------------------------------------------------------------------
}